package com.tarzan.video.service.impl;

import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.video.service.VideoService;
import com.tarzan.video.util.MinioUtil;
import com.tarzan.video.util.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author liujinshan
 * @title: VideoServiceImpl
 * @projectName com.tarzan.video.service.impl
 * @description: TODO
 * @date 2021/5/24 13:34
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * @param file
     * @return void
     * @title uploadVideo
     * @description TODO 视频上传
     * @author liujinshan
     * @data 2021/5/24 14:14
     */
    @Override
    public String uploadVideo(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String objectid = sdf.format(new Date()) + filename;
        String objectName = "video/"+objectid;
        minioUtil.putObject("tarzan",file,objectName,file.getContentType());
        log.info("文件上传成功");
        return objectid;
    }

    /**
     * @param objectName
     * @return void
     * @title deleteVideo
     * @description TODO 删除视频文件
     * @author liujinshan
     * @data 2021/6/18 16:12
     */
    @Override
    public void deleteVideo(String objectName) {
        try {
            MinioUtils.removeObject("tarzan",objectName);
        } catch (Exception e) {
            throw new BusinessException("600","视频删除失败");
        }
    }

    /**
     * @param file
     * @return java.lang.String
     * @title uploadImg
     * @description TODO 上传图片
     * @author liujinshan
     * @data 2021/8/19 15:29
     */
    @Override
    public String uploadImg(MultipartFile file) {
        String url = "";
        try {
            String filename = file.getOriginalFilename();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String objectName = "images/"+sdf.format(new Date()) + filename;
            minioUtil.putObject("tarzan",file,objectName,file.getContentType());
            log.info("文件上传成功");
            url = minioUtil.getObjectUrl("tarzan",objectName);
        } catch (Exception e) {
        }
        return url;
    }

    @Override
    public String getUrlById(String objectName) {

        String url = "";
        try {
            url = minioUtil.getObjectUrl("tarzan",objectName);
        } catch (Exception e) {
        }
        return url;
    }
}
